package com.myqm.service.impl.weixin;

import java.util.List;

import javax.sound.midi.Receiver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myqm.dao.weixin.CarDao;
import com.myqm.pojo.weixin.Car;
import com.myqm.pojo.weixin.GoodsInfo;
import com.myqm.pojo.weixin.query.QueryCar;
import com.myqm.service.weixin.CarService;

@Service
public class CarServiceImpl implements CarService {

	@Autowired
	private CarDao carDao;
	
	@Override
	public int delById(Integer customerId) {
		return carDao.delById(customerId);
	}

	@Override
	public int add(Car record) {
		Double count = carDao.get(record.getCustomerId(), record.getInternalTradeId());
		if(count == null) {
			return carDao.add(record);
		}else {
			count = count + record.getCount();
			record.setCount(count);
			return carDao.editById(record);
		}
		
	}

	@Override
	public int editById(Car record) {
		return carDao.editById(record);
	}

	@Override
	public PageInfo<Car> listByCustomerId(int pagenum, int pagesize,QueryCar param) {
		PageHelper.startPage(pagenum, pagesize);
		List<Car> datalist = carDao.listByCustomerId(param.getCustomerId()); 
		PageInfo<Car> p = new PageInfo<Car>(datalist);
		return p;
	}

	@Override
	public int del(GoodsInfo goodsInfo) {
		return carDao.del(goodsInfo.getCustomerId(), goodsInfo.getInternalTradeId());
	}

	@Override
	public List<Car> listAll(QueryCar param) {
		List<Car> datalist = carDao.listByCustomerId(param.getCustomerId()); 
		return datalist;
	}

}
